package com.lzw.bluetoothpractice.Activitys;


import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzw.bluetoothpractice.Adapters.HealthyMessagesAdapter;

import com.lzw.bluetoothpractice.Adapters.RecommendDoctorListAdapter;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.Models.Article;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.AppUtil;
import com.lzw.bluetoothpractice.Utils.SharePreferenceUtils;
import com.lzw.bluetoothpractice.bean.HealthyMessageListBean;
import com.lzw.bluetoothpractice.bean.RecommendDoctorBean;
import com.lzw.bluetoothpractice.network.HeadAssmblyUtils;
import com.lzw.bluetoothpractice.network.RetrofitManager;
import com.lzw.bluetoothpractice.network.api.RequestApi;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.lzw.bluetoothpractice.MyApplication.getContext;

public class HealthyMessagesActivity extends BaseActivity {
    HealthyMessagesAdapter healthyMessagesAdapter;
    RecyclerView rchealthyList;
    public List<Article> articleList;
    private Map<String, String> keyMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_healthy_messages);
        rchealthyList=fd(R.id.rchealthyList);
       // Bmob.initialize(this, "76f6ea7dee566f989b60f81b98cf772c");
        articleList=new ArrayList<Article>();
        String accessToken = SharePreferenceUtils.getString(this, "access_token", null);
        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/android/message/list");
        } catch (Exception e) {
            e.printStackTrace();
        }
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).getMessageList("1", AppUtil.getVersionCode() + "", accessToken, keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e("登录",e.getMessage()+"");

            }

            @Override
            public void onNext(JSONObject json) {

                Log.e("登录",json+"");
                HealthyMessageListBean homeGridBean = JSON.parseObject(json.toString(), HealthyMessageListBean.class);
                if(homeGridBean.getCode()==200){


                    //Article article=new Article();
                    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(HealthyMessagesActivity.this);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    //Article article=new Article();
                    rchealthyList.setLayoutManager(linearLayoutManager);
                    //rchealthyList.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
                    healthyMessagesAdapter=new HealthyMessagesAdapter(HealthyMessagesActivity.this);

                    healthyMessagesAdapter.updateData(homeGridBean.getRows());
                    rchealthyList.setAdapter(healthyMessagesAdapter);

                }else {
                    Toast toast=Toast.makeText(HealthyMessagesActivity.this,"查询健康资讯失败",Toast.LENGTH_LONG);
                    toast.show();
                }


            }
        });



        InitView();


    }

    private  void InitView(){
        InitNavBar2(true,"健康资讯",false);



    }
    private void queryArticle(){

    }

}
